package yhao.demo.service.login.service;


import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import yhao.demo.service.login.apilist.model.LoginPersonModel;
import yhao.demo.service.login.dao.PersonDao;
import yhao.infra.apilist.RequestResultException;
import yhao.infra.apilist.validate.LoginForm;
import yhao.infra.common.util.MD5Util;

import javax.annotation.Resource;

import static yhao.demo.service.login.apilist.ReturnCodeEnum.FOA_LOGIN_0002;
import static yhao.demo.service.login.apilist.ReturnCodeEnum.FOA_LOGIN_0003;


/**
 * Created by yoara on 2019/1/15
 */
@Service
public class LoginService {
    @Resource
    private PersonDao personDao;

    public LoginPersonModel authenticatePerson(LoginForm form) {
        LoginPersonModel loginPerson = personDao.authenticatePerson(form);
        if (loginPerson != null) {
            String pwdMd5 = StringUtils.isEmpty(form.getPassword())?"":MD5Util.getMD5(form.getPassword().getBytes());
            if (!loginPerson.getPassword().equals(pwdMd5)) {
                throw new RequestResultException(FOA_LOGIN_0002);
            }
        } else {
            throw new RequestResultException(FOA_LOGIN_0003);
        }
        //从数据库查询权限...
        //loginPerson.getCurrentAuthMap().add("...")
        return loginPerson;
    }
}
